package com.leyou.service.impl;

import com.leyou.clients.BrandClient;
import com.leyou.clients.CategoryClient;
import com.leyou.clients.GoodsClient;
import com.leyou.clients.SpecClient;
import com.leyou.item.pojo.*;
import com.leyou.service.PageService;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import javax.persistence.Id;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PageServiceImpl implements PageService {
    @Autowired
    private GoodsClient goodsClient;
    @Autowired
    private CategoryClient categoryClient;
    @Autowired
    private BrandClient brandClient;
    @Autowired
    private SpecClient specClient;

    @Override
    public Map<String, Object> queryData(Long spuId) {
        Map<String, Object> pageMap = new HashMap<>();
        //根据spuId查询spu
        Spu spu = this.goodsClient.querySpuById(spuId);
        pageMap.put("spu", spu);
        //根据spuId查询sku
        List<Sku> skus = this.goodsClient.querySkuBySpuId(spuId);
        pageMap.put("skus", skus);
        //根据spuId查询spuDetail
        SpuDetail spuDetail = this.goodsClient.querySpuDetailById(spuId);
        pageMap.put("spuDetail", spuDetail);
        //根据品牌id查询分类信息(categories)
        List<String> categories = this.categoryClient.queryNameByIds(Arrays.asList(spu.getCid1(), spu.getCid2(), spu.getCid3()));
        pageMap.put("categories", categories);
        //根据品牌id查询品牌的名称
        Brand brand = this.brandClient.queryBrandByBid(spu.getBrandId());
        pageMap.put("brand", brand);
        //查询规格参数组
        List<SpecGroup> specGroups = this.specClient.querySpecGroups(spu.getCid3());
        pageMap.put("specGroups", specGroups);
        //查询特有规格参数(既然查询的是特有规格参数,那么最后一个参数就是false,当时true的时候表示的是通用规格参数)
        List<SpecParam> specParams = this.specClient.querySpecParams(null, spu.getCid3(), null, false);
        //声明一个map用于存放特有规格参数
        Map<Long, Object> specParamMap = new HashMap<>();
        specParams.forEach(specParam -> {
            Long id = specParam.getId();
            String name = specParam.getName();
            specParamMap.put(id, name);
        });
        pageMap.put("specParam", specParamMap);
        return pageMap;
    }
}
